﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer;
using BusinessLogicLayer;
using System.Globalization;

namespace ChamCong.Admin
{
    public partial class QuanLyXetCong : BasePage
    {
        XetCongBLL xcBLL = new XetCongBLL();
        CongBLL cBLL = new CongBLL();
        TangCaBLL tcBLL = new TangCaBLL();
        LoaiCongBLL lcBLL = new LoaiCongBLL();
        NhanVienBLL nvBLL = new NhanVienBLL();
        PhanQuyenBLL pq = new PhanQuyenBLL();
        QuanLyPhanQuyenBLL q = new QuanLyPhanQuyenBLL();
        public static int ma, range, _ttd;
        public static int _MaPB, _MaNV;

        //int _ttd;
        protected void Page_Load(object sender, EventArgs e)
        {
            tbShow.Visible = false;
            _ttd = Convert.ToInt32(Request.QueryString["ttd"]);
            try
            {
                range = Convert.ToInt32(Request.QueryString["pv"]);
                if (_ttd != 0)
                {
                    setButton(false);
                }
                if (!IsPostBack)
                {
                    _MaNV = Convert.ToInt32(Session["MaNV"]);
                    _MaPB = nvBLL.TimPhongBan(_MaNV);

                    bindGridView(_MaNV, LayLoaiDS(), range);
                }
            }
            catch (Exception ex)
            {
                sbcMsgBox1.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Error, "Thông báo", ex.Message);
            }

        }
        public int LayLoaiDS()
        {
            int ttd;
            if (Request.QueryString["ttd"] != null)
            {
                ttd = Convert.ToInt32(Request.QueryString["ttd"]);
            }
            else
            {
                ttd = 3;
            }
            return ttd;

        }
        public void bindGridView(int _manv, int _ttd, int _pv)
        {
            
            List<XETCONG2> ls0;
            if (_pv == 0)
            {
                ls0 = xcBLL.LayDSXetCong(0, _MaPB, _ttd, _pv);
            }
            else
            {
                ls0 = xcBLL.LayDSXetCong(_manv, _MaPB, _ttd, _pv);
            }
            if (ls0.Count != 0)
            {
                dgvXetCong0.DataSource = ls0;
                dgvXetCong0.DataBind();
            }
            else
            {
                sbcMsgBox1.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Warning, "Thông báo", "Hiện thời không có dữ liệu!");
            }

        }

        protected void dgvXetCong0_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton imgbtXem = (ImageButton)e.Row.FindControl("imgbtXem0");

                if (imgbtXem != null)
                    imgbtXem.Attributes["Ma"] = e.Row.Cells[0].Text;
            }
        }

        protected void imgbtXem0_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton btn = (ImageButton)sender;
            ma = int.Parse(btn.Attributes["Ma"]);
            ShowThongTinXetCong(ma);
            if (_ttd != 0)
            {
                setButton(false);
            }
        }


        protected void ShowThongTinXetCong(int ma)
        {
            tbShow.Visible = true;
            XETCONG a = xcBLL.LayXetCong(ma);
            lbMaCong2.Text = a.MaLoaiCong;
            lbMaNV.Text = a.MaNV.ToString();
            lbHoTen.Text = a.NHANVIEN.HoTen.ToString();
            lbGioTangCa2.Text = a.TGTangCa.ToString();
            lbHeSo2.Text = a.HeSoTC.ToString();
            txtLyDo.Text = a.LyDo;
            txtChuThich.Text = a.ChuThich;
            txtLyDo.Enabled = false;
            lbNgay.Text = String.Format("{0:dd/MM/yyyy}", a.Ngay);

            DateTime _ngay = a.Ngay.Value;
            int _manv = (int)a.MaNV;

            CONG c = cBLL.LayCong(_manv, _ngay);
            if (c == null)
            {
                lbGioDen.Text = "";
                lbGioVe.Text = "";
                lbSoCong.Text = "0";
                lbMaCong.Text = "0";
            }
            else
            {
                lbGioDen.Text = String.Format("{0:T}", c.GioDen);
                lbGioVe.Text = String.Format("{0:T}", c.GioVe);
                lbSoCong.Text = c.SoCong.ToString();
                lbMaCong.Text = c.MaLoaiCong;
            }

            TANGCA t = tcBLL.LayTC(_ngay, _manv);
            if (t == null)
                lbGioTangCa.Text = "";
            else
                lbGioTangCa.Text = t.ThoiGian.ToString();
        }

        protected void btnChapNhan_Click(object sender, EventArgs e)
        {
            IFormatProvider format = new CultureInfo("vi-VN");
            DateTime _ngay = (DateTime)Convert.ToDateTime(lbNgay.Text, format);
            int _manv = int.Parse(lbMaNV.Text);
            try
            {
                if (lbGioTangCa2.Text == "")
                {
                }
                else if (lbGioTangCa2.Text.Trim() == "0")
                {
                    tcBLL.Xoa(_ngay, _manv);
                }
                else
                {
                    TANGCA a = new TANGCA();
                    a.MaNV = _manv;
                    a.Ngay = _ngay;
                    a.ThoiGian = Convert.ToDouble(lbGioTangCa2.Text);
                    a.HeSo = Convert.ToDouble(lbHeSo2.Text);
                    a.TrangThaiChot = 0;
                    tcBLL.Sua(a);
                }

                if (lbMaCong2.Text.Trim() == "")
                {
                    cBLL.XoaCong(_ngay, _manv);
                }
                else
                {
                    CONG c = new CONG();
                    c.Ngay = _ngay;
                    c.MaNV = _manv;
                    c.MaLoaiCong = lbMaCong2.Text.Trim();
                    c.SoCong = lcBLL.LaySoCong(c.MaLoaiCong);
                    c.TrangThaiChot = 0;
                    cBLL.SuaCong(c);
                }


                XETCONG x = new XETCONG();
                x.MaXetCong = ma;
                x.LyDo = txtLyDo.Text;
                x.MaNV = _manv;
                x.Ngay = _ngay;
                x.TrangThaiDuyet = 1;
                x.ChuThich = txtChuThich.Text;
                x.TrangThaiDoc = 0;
                xcBLL.Sua(x);
                tbShow.Visible = false;
                bindGridView(_MaNV, LayLoaiDS(), range);
            }
            catch (Exception ex)
            {
                //lbThongBao.Text = ex.ToString();
                sbcMsgBox1.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Error, "Thông báo", ex.Message);
            }
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {
            if (txtChuThich.Text == "")
            {
                sbcMsgBox1.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Error, "Thông báo", "Vui lòng để lại phản hồi vào ô chú thích!");
            }
            else
            {
                IFormatProvider format = new CultureInfo("vi-VN");
                DateTime _ngay = (DateTime)Convert.ToDateTime(lbNgay.Text, format);
                int _manv = int.Parse(lbMaNV.Text);

                XETCONG x = new XETCONG();
                x.MaXetCong = ma;
                x.MaNV = _manv;
                x.Ngay = _ngay;
                x.TrangThaiDuyet = 2;
                x.ChuThich = txtChuThich.Text;
                x.TrangThaiDoc = 0;
                xcBLL.Sua(x);
                tbShow.Visible = false;
                bindGridView(_MaNV, LayLoaiDS(), range);

            }

        }

        public string ktTrangThaiDuyet(string _ttd)
        {
            return _ttd;    
        }

        private void setButton(bool _flag)
        {
            btnChapNhan.Visible = _flag;
            btnHuy.Visible = _flag;
            txtChuThich.Enabled = false;
        }

        protected void dgvXetCong0_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            dgvXetCong0.PageIndex = e.NewPageIndex;
            bindGridView(_MaNV, LayLoaiDS(), range);
        }
    }
}